package vg.modules.tab_closer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Observable;
import java.util.Set;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventCloseTab;
import vg.services.plugin_manager.event_and_request.event.UIEventOpenNewTab;
import vg.services.plugin_manager.event_and_request.request.UIRequestCloseCurrentTab;
import vg.services.plugin_manager.event_and_request.request.UIRequestCloseTabSet;
import vg.services.user_interface_manager.interfaces.AMenuItem;
import vg.services.user_interface_manager.interfaces.AUserInterface;

public class SimpleTabCloser {
	// Main data
	private final Set<Integer> tabSet;
	
	// Main components
	private final JMenuItem       closeTabMenuItem;
	private final JMenuItem       closeAllTabMenuItem;
	/**
	 * Constructor.
	 * @param userInterface - feedback between this plugin and system.
	 */
	public SimpleTabCloser(final AUserInterface userInterface) {
		// initialize data
		this.tabSet = new HashSet<Integer>();
		
		// initialize components
		this.closeTabMenuItem = new JMenuItem("Close tab"); 
		this.closeTabMenuItem.setIcon(new ImageIcon("./data/resources/textures/closeTab.png"));
		this.closeTabMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.CTRL_DOWN_MASK));

		this.closeAllTabMenuItem = new JMenuItem("Close all tabs"); 
		//this.closeAllTabMenuItem.setIcon(new ImageIcon("./data/resources/textures/closeTab.png"));

		// initialize listeners
		this.closeTabMenuItem.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				UIRequestCloseCurrentTab req = new UIRequestCloseCurrentTab(null);
				userInterface.addRequest(req);
			}
		});
		
		this.closeAllTabMenuItem.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				synchronized (tabSet) {
					Set<Integer> tmpTabSet = new HashSet<Integer>(tabSet);
					UIRequestCloseTabSet req = new UIRequestCloseTabSet(null, tmpTabSet);
					userInterface.addRequest(req);			
				}
			}
		});
		
		userInterface.addMenuItem(new AMenuItem(this.closeTabMenuItem) {
			public void update(Observable o, Object arg) {
				// empty, because we state update method for closing all tab menu item 
			}
		}, "File");
		
		userInterface.addMenuItem(new AMenuItem(this.closeAllTabMenuItem) {
			public void update(Observable o, Object arg) {
				if (arg instanceof AUIEvent) {
					AUIEvent event = (AUIEvent) arg;
					switch (event.getType()) {
						case DEF_OPEN_NEW_TAB: {
							UIEventOpenNewTab bufEvent = (UIEventOpenNewTab)event;
							synchronized (tabSet) {
								tabSet.add(bufEvent.getTabId());
							}
							break;
						}
						
						case DEF_CLOSE_TAB: {
							UIEventCloseTab bufEvent = (UIEventCloseTab)event;
							synchronized (tabSet) {
								tabSet.remove(bufEvent.getTabId());
							}
							break;					
						}
						
						default:break;
					}
				}
			}
		}, "File");
	}
}
